package it.vidoc.controller;

import it.vidoc.mybatis.javamodel.Anagrafiche;
import it.vidoc.mybatis.javamodel.Effetti;
import it.vidoc.mybatis.javamodel.Infcomuni;
import it.vidoc.mybatis.sqlquery.SqlAnagrafiche;
import it.vidoc.mybatis.sqlquery.SqlInfComuni;
import it.vidoc.mybatis.sqlquery.SqlEffetti;
import it.vidoc.utils.AmCercaSoggetto;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.zkoss.image.Image;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Panel;
import org.zkoss.zul.Textbox;

@SuppressWarnings("rawtypes")
public class WinAmRicercaController extends GenericForwardComposer {
	private static final long serialVersionUID = 6204566952879868705L;
	private String alberto;
	private List<Effetti> listEffetti = null;
	private ArrayList<Integer> result = null;
	private Long startQuery = 0L;
	private Long endQuery = 0L;
	private Integer totPagLis = 0;
	private Integer totPagVis = 0;
	
	private Textbox txbNom;
	private Combobox cmbPrv;
	private Combobox cmbCom;
	private Textbox txbSprv;
	private Panel pnlAMRicerca;
	private Panel pnlAMLista;
	private Panel pnlAMVisura;
	private Listbox lbListaAnag;
	private Listbox lbVisura;
	private Label lblNumSogg;
	private Label lblNumSogg0;
	private Intbox inbNpagLis;
	private Intbox inbNpagVis;
	private Label lblFoo1Lis;
	private Label lblFoo1Vis;
	private Image imgIniRic;
	
	@SuppressWarnings("unchecked")
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
	}


//	public void onClick$btnIniRic(Event event) throws IOException{
	public void onClick$imgIniRic(Event event) throws IOException{		
		if (txbNom.getValue().length() < 2) {
			Clients.showNotification("Nominativo obbligatorio", "warning", null, null, 0, true);			
			return;
		}
		if (txbSprv.getValue().toString().length() > 0) {
			List<Infcomuni> lstPrv = new SqlInfComuni().GetComuni(txbSprv.getValue().toString());
			if (lstPrv.size() < 1) {
				Clients.showNotification("Sigla provincia errata o omessa", "warning", null, null, 0, true);
				return;
			}
		}
		
		
		startQuery = System.currentTimeMillis();
		result = new AmCercaSoggetto().AmCercaSoggetto(txbNom.getValue().toString(), "", txbSprv.getValue().toString(), cmbCom.getSelectedItem().getValue().toString() );
		endQuery = System.currentTimeMillis();
		String durationQuery = String.valueOf((endQuery - startQuery) / 1000) + "," + String.valueOf((endQuery - startQuery) % 1000);		
		if (result.size() > 0) {
			riempiLbAnagra(1);
			pnlAMLista.setOpen(true);
			pnlAMLista.setVisible(true);
			lblNumSogg.setValue("(N. soggetti trovati: " + result.size() + " in " + durationQuery + " secondi)");
			pnlAMRicerca.setOpen(false);
			pnlAMRicerca.setVisible(false);
		} else {
			lblNumSogg0.setValue("(N. soggetti trovati: " + result.size() + " in " + durationQuery + " secondi)");
			Clients.showNotification("Nessun soggeto trovato con i dati indicati", "warning", null, null, 0, true);
		}		
	}

   public void riempiLbAnagra(Integer pagina) {
		pnlAMLista.setOpen(true);
		lbListaAnag.getItems().clear();
		DecimalFormat decimalFormat = new DecimalFormat( "#,###,###,##0.00" );
		
		Integer iniLb = 0;
		Integer finLb = 0;
		if (pagina == 1) {
			iniLb = 0;
			finLb = 12;
		} else {
			iniLb = ((pagina - 1) * 12);
		}
		finLb = iniLb + 12;
		if (finLb > result.size()) {
			finLb = result.size();
		}
		
		for (int i = iniLb; i < finLb; i++) {
			Anagrafiche anagrafiche = new SqlAnagrafiche().GetPKAnagrafiche(result.get(i));
			Listitem riga = new Listitem();
			
			Listcell cella = new Listcell();
			cella.setLabel(String.valueOf(i + 1));
			riga.appendChild(cella);

			cella = new Listcell();
			cella.setLabel(String.valueOf(anagrafiche.getKanagra()));
			riga.appendChild(cella);
			
			cella = new Listcell();
			cella.setLabel(anagrafiche.getNominativo());
			riga.appendChild(cella);		

			cella = new Listcell();
			cella.setLabel(anagrafiche.getCodicefiscale());
			riga.appendChild(cella);		

			cella = new Listcell();
			cella.setLabel(anagrafiche.getIndirizzoresidenza());
			riga.appendChild(cella);		

//			cella = new Listcell();
//			cella.setLabel(anagrafiche.getSiglaprovinciaresidenza());
//			riga.appendChild(cella);		

			
			cella = new Listcell();			
			List<Infcomuni> lstCom = new SqlInfComuni().GetComune(anagrafiche.getCodistatprvres() + anagrafiche.getCodistatcomres() );
			if (lstCom.size() > 0) {
				cella.setLabel(lstCom.get(0).getDenomcomune().trim() + " (" + anagrafiche.getSiglaprovinciaresidenza() + ")");
			}
			riga.appendChild(cella);
			
			
			cella = new Listcell();
			cella.setLabel(anagrafiche.getDataulteff().substring(6, 8) + "/" + anagrafiche.getDataulteff().substring(4, 6) + "/" + anagrafiche.getDataulteff().substring(0, 4));
			
			riga.appendChild(cella);		

			cella = new Listcell();
			cella.setLabel(String.valueOf(anagrafiche.getNumtoteff()));
			riga.appendChild(cella);		
			
			cella = new Listcell();
			cella.setLabel(String.valueOf(decimalFormat.format(anagrafiche.getImptoteff())));
			
			riga.appendChild(cella);		

			
			lbListaAnag.appendChild(riga);
		 }
		
		inbNpagLis.setValue(pagina);
		totPagLis = 0;
		if ((result.size() % 12) == 0) {
			totPagLis = result.size() / 12;
		} else {
			totPagLis = (result.size() / 12) + 1;
		}
		if (totPagLis == 0) {
			totPagLis = 1;
		}
		
		lblFoo1Lis.setValue("di " + totPagLis);
   }  
	

   public void riempiLbVisura(Integer pagina) {
		pnlAMLista.setVisible(false);
		pnlAMVisura.setVisible(true);
	   
		lbVisura.getItems().clear();
		DecimalFormat decimalFormat = new DecimalFormat( "#,###,###,##0.00" );
		
		Integer iniLb = 0;
		Integer finLb = 0;
		if (pagina == 1) {
			iniLb = 0;
			finLb = 12;
		} else {
			iniLb = ((pagina - 1) * 12);
		}
		finLb = iniLb + 12;
		if (finLb > listEffetti.size()) {
			finLb = listEffetti.size();
		}
		
		for (int i = iniLb; i < finLb; i++) {
			Listitem riga = new Listitem();
			
			Listcell cella = new Listcell();
			cella.setLabel(String.valueOf(i + 1));
			riga.appendChild(cella);

			cella = new Listcell();
			cella.setLabel(listEffetti.get(i).getTipoeffetto());
			if (listEffetti.get(i).getTipoeffetto().trim().equals("A")) {
				cella.setLabel("Assegno");
			}
			if (listEffetti.get(i).getTipoeffetto().trim().equals("C")) {
				cella.setLabel("Cambiale");
			}
			if (listEffetti.get(i).getTipoeffetto().trim().equals("T")) {
				cella.setLabel("Tratta");
			}

			riga.appendChild(cella);
			
			cella = new Listcell();
			cella.setLabel(listEffetti.get(i).getDatalevata().substring(6, 8) + "/"
					+ listEffetti.get(i).getDatalevata().substring(4, 6) + "/"
					+ listEffetti.get(i).getDatalevata().substring(0, 4));
			riga.appendChild(cella);		

			cella = new Listcell();
			cella.setLabel(listEffetti.get(i).getDataiscrizione().substring(6, 8) + "/"
					+ listEffetti.get(i).getDataiscrizione().substring(4, 6) + "/"
					+ listEffetti.get(i).getDataiscrizione().substring(0, 4));
			riga.appendChild(cella);		

			cella = new Listcell();
			cella.setLabel(listEffetti.get(i).getCciaapubblicazione());
			riga.appendChild(cella);
			
			cella = new Listcell();
			if (listEffetti.get(i).getCodicevaluta().trim().equals("EU")) {
				cella.setLabel(String.valueOf(decimalFormat.format(listEffetti.get(i).getImporto())));
			} else {
				cella.setLabel(String.valueOf(decimalFormat.format(listEffetti.get(i).getImportocorrente())));
			}
			riga.appendChild(cella);		
			
			lbVisura.appendChild(riga);
		 }
		inbNpagVis.setValue(pagina);
		totPagVis = 0;
		if ((listEffetti.size() % 12) == 0) {
			totPagVis = listEffetti.size() / 12;
		} else {
			totPagVis = (listEffetti.size() / 12) + 1;
		}
		if (totPagVis == 0) {
			totPagVis = 1;
		}
		
		lblFoo1Vis.setValue("di " + totPagVis);
   }  

   
   
	public void onClick$btnNewRic(Event event) throws IOException{
		pnlAMLista.setVisible(false);
		pnlAMRicerca.setOpen(true);
		pnlAMRicerca.setVisible(true);
	}

	public void onClick$btnNewVis(Event event) throws IOException{
		pnlAMLista.setVisible(true);
		pnlAMLista.setOpen(true);
		pnlAMRicerca.setOpen(false);
		pnlAMRicerca.setVisible(false);
		pnlAMVisura.setOpen(false);
		pnlAMVisura.setVisible(false);
		lbListaAnag.clearSelection();
	}

	public void onClick$btnGoPageLis(Event event) throws IOException{
		if (inbNpagLis.getValue() < 1) {
			inbNpagLis.setValue(1);
		}
		if (inbNpagLis.getValue() > totPagLis) {
			inbNpagLis.setValue(totPagLis);
		}

		riempiLbAnagra(inbNpagLis.getValue());
		
	}
	
	public void onClick$btnPagPrecLis(Event event) {
		if ((inbNpagLis.getValue() - 1) < 1) {
			inbNpagLis.setValue(1);
		} else {
			inbNpagLis.setValue((inbNpagLis.getValue() - 1));
		}
		
		riempiLbAnagra(inbNpagLis.getValue());
		
	}

	public void onClick$btnPagSuccLis(Event event) {
		if ((inbNpagLis.getValue() + 1) > totPagLis) {
			inbNpagLis.setValue(totPagLis);
		} else {
			inbNpagLis.setValue((inbNpagLis.getValue() + 1));
		}
		
		riempiLbAnagra(inbNpagLis.getValue());
		
		
	}
	
	public void onClick$btnGoPageVis(Event event) throws IOException{
		if (inbNpagVis.getValue() < 1) {
			inbNpagVis.setValue(1);
		}
		if (inbNpagVis.getValue() > totPagVis) {
			inbNpagVis.setValue(totPagVis);
		}

		riempiLbVisura(inbNpagVis.getValue());
		
	}
	
	public void onClick$btnPagPrecVis(Event event) {
		if ((inbNpagVis.getValue() - 1) < 1) {
			inbNpagVis.setValue(1);
		} else {
			inbNpagVis.setValue((inbNpagVis.getValue() - 1));
		}
		riempiLbVisura(inbNpagVis.getValue());
	}

	public void onClick$btnPagSuccVis(Event event) {
		if ((inbNpagVis.getValue() + 1) > totPagVis) {
			inbNpagVis.setValue(totPagVis);
		} else {
			inbNpagVis.setValue((inbNpagVis.getValue() + 1));
		}
		riempiLbVisura(inbNpagVis.getValue());
	}


	public void onSelect$lbListaAnag(Event event) {
        Listcell listcell = (Listcell)lbListaAnag.getSelectedItem().getChildren().get(1);
        String cella = listcell.getLabel();

        listEffetti = new SqlEffetti().GetEffetti(Integer.valueOf(cella));
        
		riempiLbVisura(1);
		
		pnlAMVisura.setOpen(true);
		pnlAMVisura.setVisible(true);
	}

	
	public void onCreate() throws IOException {
		pnlAMLista.setOpen(false);
		pnlAMLista.setVisible(false);
		pnlAMVisura.setOpen(false);
		pnlAMVisura.setVisible(false);

		List<Infcomuni> lstPrv = new SqlInfComuni().GetProvincie();
		cmbPrv.getItems().clear();
		Comboitem ciPrv = new Comboitem();
		ciPrv.setLabel("-----");
		cmbPrv.appendChild(ciPrv);
		for (int i = 0; i < lstPrv.size(); i++) {
			ciPrv = new Comboitem();
			ciPrv.setLabel(lstPrv.get(i).getDenomcomune());
			ciPrv.setValue(lstPrv.get(i).getSiglaprovincia());
			cmbPrv.appendChild(ciPrv);
		}
		cmbPrv.setSelectedIndex(0);
		
		cmbCom.getItems().clear();
		Comboitem ciCom = new Comboitem();
		ciCom.setLabel("-----");
		cmbCom.appendChild(ciCom);
		cmbCom.setSelectedIndex(0);
   }
	
   public void onChanging$txbNom(Event event) {
	   lblNumSogg0.setValue("");
	   lbListaAnag.getItems().clear();
	   pnlAMLista.setOpen(false);
	   pnlAMLista.setVisible(false);	   
   }
   
   public void onSelect$cmbPrv(Event event){
	   if ((cmbPrv.getSelectedItem() == null) || (cmbPrv.getSelectedItem().getLabel().equals("-----"))) {
			cmbCom.getItems().clear();
			Comboitem ciCom = new Comboitem();
			ciCom.setLabel("-----");
			ciCom.setValue("-----");
			cmbCom.appendChild(ciCom);
			cmbCom.setSelectedIndex(0);
			txbSprv.setValue("");
	   } else {
			List<Infcomuni> lstCom = new SqlInfComuni().GetComuni(cmbPrv.getSelectedItem().getValue().toString());
			cmbCom.getItems().clear();
			Comboitem ciCom = new Comboitem();
			ciCom.setLabel("-----");
			ciCom.setValue("-----");
			cmbCom.appendChild(ciCom);
			for (int i = 0; i < lstCom.size(); i++) {
				ciCom = new Comboitem();
				ciCom.setLabel(lstCom.get(i).getDenomcomune());
				ciCom.setValue(lstCom.get(i).getInfcodicecomune());
				cmbCom.appendChild(ciCom);
			}
			cmbCom.setSelectedIndex(0);
		   
			txbSprv.setValue(cmbPrv.getSelectedItem().getValue().toString());
	   }
   }
   
   
   public void onChanging$cmbPrv(Event event){
		cmbCom.getItems().clear();
		Comboitem ciCom = new Comboitem();
		ciCom.setLabel("-----");
		cmbCom.appendChild(ciCom);
		cmbCom.setSelectedIndex(0);
		txbSprv.setValue("");
   }

   public void onChanging$txbSprv(Event event){
		cmbPrv.setSelectedIndex(0);
	   
		cmbCom.getItems().clear();
		Comboitem ciCom = new Comboitem();
		ciCom.setLabel("-----");
		cmbCom.appendChild(ciCom);
		cmbCom.setSelectedIndex(0);
   }

   
}
